package thread;
//当多个线程并发操作同一临界资源由于线程切换不确定，导致顺序出现了混乱产生的不良后果
//
public class SyncDemo01 {
    public static void main(String[] args) {
    Table table=new Table();
    Thread t1=new Thread(){
        public void run(){
            while (true){
                int bean=table.getBean();
                Thread.yield();
                System.out.println(getName()+":"+bean);
            }
        }
    };
        Thread t2=new Thread(){
            public void run(){
                while (true){
                    int bean=table.getBean();
                    Thread.yield();
                    System.out.println(getName()+":"+bean);
                }
            }
        };
    t1.start();
    t2.start();

    }
}
class Table{
    private int beans=20;
    public  synchronized int getBean(){
        if (beans==0){
            throw new RuntimeException("没有豆子了");
        }
        Thread.yield();
        return beans--;
    }
}